package smartlifev30.com.sdk.api.gateway_app;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.api.ConstantGateway;
import smartlifev30.com.sdk.bean.request.AddDeviceRequest;
import smartlifev30.com.sdk.bean.request.CmdList;
import smartlifev30.com.sdk.bean.request.DelDeviceRequest;
import smartlifev30.com.sdk.bean.request.DeviceQueryRequest;
import smartlifev30.com.sdk.bean.request.EditDeviceRequest;
import smartlifev30.com.sdk.bean.request.QueryDeviceCmdRequest;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.baiwei.ResponseHelper;

/**
 * Created by tys on 2018/8/13.
 */

public class DeviceMgmtGatewayImpl implements IDeviceMgmtGateway {

    @Override
    public void deviceQuery(@NonNull String userName, @NonNull String token, @NonNull String sn,
                            @Nullable List<String> typeList, @NonNull CallBack<LoginResponse> callBack) {
        DeviceQueryRequest deviceQueryRequest = new DeviceQueryRequest();
        deviceQueryRequest.setFrom(userName);
        deviceQueryRequest.setTo(sn);
        deviceQueryRequest.setMsg_id(callBack.getMsgId());
        deviceQueryRequest.setMsg_class(ConstantGateway.MSG_CLASS_DEVICE);
        deviceQueryRequest.setMsg_name("device_query");
        deviceQueryRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        deviceQueryRequest.setToken(token);
        List<DeviceQueryRequest.TypeList> protectType = null;
        if (typeList != null) {
            protectType = new ArrayList<>();
            for (String type : typeList) {
                protectType.add(deviceQueryRequest.new TypeList(type));
            }
        }
        deviceQueryRequest.setType_list(protectType);
        NetManager.getInstance().sendMessage(JsonUtil.toString(deviceQueryRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void addDevice(@NonNull String userName, @NonNull String token, @NonNull String sn,
                          @NonNull String protectType, @NonNull String deviceAttr, @NonNull String deviceName,
                          @NonNull Integer protectId, @NonNull String roomId, @NonNull CallBack<LoginResponse> callBack) {
        AddDeviceRequest addDeviceRequest = new AddDeviceRequest();
        addDeviceRequest.setFrom(userName);
        addDeviceRequest.setTo(sn);
        addDeviceRequest.setMsg_id(callBack.getMsgId());
        addDeviceRequest.setMsg_class(ConstantGateway.MSG_CLASS_DEVICE);
        addDeviceRequest.setMsg_name("device_add");
        addDeviceRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        addDeviceRequest.setToken(token);
        AddDeviceRequest.Device device = addDeviceRequest.new Device();
        device.setProduct_type(protectType);
        device.setDevice_attr(deviceAttr);
        device.setDevice_name(deviceName);
        device.setProduct_id(protectId);
        device.setRoom_id(roomId);
        addDeviceRequest.setDevice(device);
        NetManager.getInstance().sendMessage(JsonUtil.toString(addDeviceRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void delDevice(@NonNull String userName, @NonNull String token, @NonNull String sn,
                          @Nullable List<Integer> deviceList, @NonNull CallBack<LoginResponse> callBack) {
        DelDeviceRequest delDeviceRequest = new DelDeviceRequest();
        delDeviceRequest.setFrom(userName);
        delDeviceRequest.setTo(sn);
        delDeviceRequest.setMsg_id(callBack.getMsgId());
        delDeviceRequest.setMsg_class(ConstantGateway.MSG_CLASS_DEVICE);
        delDeviceRequest.setMsg_name("device_del");
        delDeviceRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        delDeviceRequest.setToken(token);
        List<DelDeviceRequest.DeviceList> list = null;
        if (deviceList != null) {
            list = new ArrayList<>();
            for (Integer deviceId : deviceList) {
                list.add(delDeviceRequest.new DeviceList(deviceId));
            }
        }
        delDeviceRequest.setDevice_list(list);
        NetManager.getInstance().sendMessage(JsonUtil.toString(delDeviceRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void editDevice(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull Integer deviceId,
                           @NonNull String deviceName, @NonNull String deviceAttr, @NonNull Integer roomId,
                           @Nullable List<CmdList> cmdList, @NonNull CallBack<LoginResponse> callBack) {
        EditDeviceRequest editDeviceRequest = new EditDeviceRequest();
        editDeviceRequest.setFrom(userName);
        editDeviceRequest.setTo(sn);
        editDeviceRequest.setMsg_id(callBack.getMsgId());
        editDeviceRequest.setMsg_class(ConstantGateway.MSG_CLASS_DEVICE);
        editDeviceRequest.setMsg_name("device_edit");
        editDeviceRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        editDeviceRequest.setToken(token);
        editDeviceRequest.setCmd_list(cmdList);
        NetManager.getInstance().sendMessage(JsonUtil.toString(editDeviceRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void queryDeviceCmd(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull Integer deviceId, @NonNull CallBack<LoginResponse> callBack) {
        QueryDeviceCmdRequest queryDeviceCmdRequest = new QueryDeviceCmdRequest();
        queryDeviceCmdRequest.setFrom(userName);
        queryDeviceCmdRequest.setToken(sn);
        queryDeviceCmdRequest.setMsg_id(callBack.getMsgId());
        queryDeviceCmdRequest.setMsg_class(ConstantGateway.MSG_CLASS_DEVICE);
        queryDeviceCmdRequest.setMsg_name("device_cmd_query");
        queryDeviceCmdRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        queryDeviceCmdRequest.setToken(token);
        queryDeviceCmdRequest.setDevice(queryDeviceCmdRequest.new Device(deviceId));
        NetManager.getInstance().sendMessage(JsonUtil.toString(queryDeviceCmdRequest));
        ResponseHelper.addListen(callBack);
    }


}
